<?php 
  require_once("includes/config.php"); 
  $length = 20;
  
  if( isset($_POST['page'])  && !empty($_POST['page'] ))
  {
    $page =  filter_var($_POST['page'], FILTER_SANITIZE_NUMBER_INT);
    $start = $page*20 - 20;
  }
  else
  {
    $start = 0;
    $page = 1;
  }
  
  $sql = 'SELECT * FROM contracts 
  LEFT JOIN clients ON 
    contracts.client_id=clients.id 
  LEFT JOIN apartments ON 
    contracts.apartment_id=apartments.id 
  LEFT JOIN companies ON 
    contracts.company_id=companies.company_id 
  
  WHERE contracts.owner_id = ?';
  
  $sqlCount = 'SELECT COUNT(*) FROM contracts 
  LEFT JOIN clients ON 
    contracts.client_id=clients.id 
  LEFT JOIN apartments ON 
    contracts.apartment_id=apartments.id 
  WHERE contracts.owner_id = ?';
  
  $binds = array();
    $binds['owner_id'] = $_SESSION['user_id'];
	if(!empty($_POST['from']) && !empty($_POST['to']))
  {
    $binds['from'] = $_POST['from'];
    $binds['to'] = $_POST['to'];
    $sql .= " and mildai_from between ? AND ?";
    $sqlCount .= " and mildai_from between ? AND ?";
  }
  $sql .= " LIMIT ?,?";
  $binds['start'] = $start;
  $binds['end'] = $length;
  
  //dump($sql);
  $contracts = query($sql,$binds);
  $Count = query($sqlCount,$binds);
  $totalPages = $Count[0]['COUNT(*)'];
  
  $additonalLink[0] = '<script src="js/edit.js" type="text/javascript" ></script>';
  
   
    render("booking_reports.php", array("title"=>"تقاريرحجوزات العملاء","contracts"=>$contracts,"page"=>$page,"totalPages"=>$totalPages,"start"=>$start,"length"=>$length,"binds"=>$binds));
  
?>